#ifndef TILEDESIGNERSCENE_H
#define TILEDESIGNERSCENE_H

#include <QGraphicsScene>
#include <QList>

#include "layer.h"

class QPoint;
class QImage;
class QColor;
class QPixmap;

struct TileInfo
{
    qint32 tileX;
    qint32 tileY;
};

class TileDesignerScene : public QGraphicsScene
{
public:
    TileDesignerScene(QObject *parent = NULL);
    ~TileDesignerScene();
    void drawForeground(QPainter *painter, const QRectF &rect);
    void drawBackground(QPainter *painter, const QRectF &rect);
    QImage save();
    void create(QColor colour, qint32 tileX, qint32 tileY, qint32 sizeX, qint32 sizeY, QString tileSetFilename);
    void addLayer(QString name);

    QColor getDefaultColour() { return defaultColour; }
    qint32 getTileSizeX() { return tileSizeX; }
    qint32 getTileSizeY() { return tileSizeY; }
    qint32 getMapSizeX() { return mapSizeX; }
    qint32 getMapSizeY() { return mapSizeY; }
    qint32 getSelectedTile() { return selectedTile; }
    void setSelectedTile(qint32 value) { selectedTile = value; }
    void setCurrentLayer(qint16 value) { currentLayer = value; }
    void setLayerVisible(qint16 layer, bool value);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);

private:
    TileInfo getTileInfo(QPointF position);

    void drawTile(qint16 layer, QPainter *painter, qint32 &i, qint32 &j, QRect &destRect);
    void processTile(qint16 layer, QPointF tileAt, qint32 tileNumber);
    QRect tileRect(int x, int y) const;
    QRect tileRect(int tileNum) const;

    QList<Layer> *layers;
    QColor defaultColour;
    qint32 tileSizeX;
    qint32 tileSizeY;
    qint32 mapSizeX;
    qint32 mapSizeY;
    qint32 selectedTile;
    qint16 currentLayer;
    bool editing;
    bool adding;

    QPixmap *tileSet;
};

#endif // TILEDESIGNERSCENE_H
